package cc.itez.tool.easyshutdown.swing;

import cc.itez.tool.easyshutdown.Logger;
import cc.itez.tool.easyshutdown.mould.Option;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.util.List;

public class Setting {
    private static final File configFile = new File("config.json");

    /**
     * 主窗口
     */
    private final JFrame mainFrame;

    public Setting(JFrame mainFrame) {
        this.mainFrame = mainFrame;
    }

    /**
     * 从磁盘读取配置
     */
    public void load() {

    }

    /**
     * 将配置写入磁盘
     */
    public void dump() {

    }


    public void popup() {
        JDialog settingsDialog = new JDialog(mainFrame, "设置", true);
        settingsDialog.setLayout(new BoxLayout(settingsDialog.getContentPane(), BoxLayout.Y_AXIS));
        settingsDialog.setSize(200, 250);
        settingsDialog.setLocationRelativeTo(mainFrame);

        JPanel optionPanel = new JPanel();
        optionPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        optionPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 30, 5));
        settingsDialog.add(optionPanel);

        // 1. 铃声选项
        JPanel soundPanel = new JPanel();

        JComboBox<Option<String>> soundComboBox = new JComboBox<>();
        soundComboBox.setRenderer(new DefaultListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                c.setPreferredSize(new Dimension(100, 20));
                return c;
            }
        });
        loadSounds().forEach(soundComboBox::addItem);
        soundPanel.add(new JLabel("铃声:"));
        soundPanel.add(soundComboBox);
        optionPanel.add(soundPanel);

        // 2. 语言选项
        JPanel languagePanel = new JPanel();
        languagePanel.setLayout(new FlowLayout());

        JComboBox<Option<String>> languageComboBox = new JComboBox<>();
        this.loadLocales().forEach(languageComboBox::addItem);

        languagePanel.add(new JLabel("语言:"));
        languagePanel.add(languageComboBox);

        optionPanel.add(languagePanel);

        JPanel infoPanel = new JPanel();
        infoPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        infoPanel.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10));
        settingsDialog.add(infoPanel);

        // 3. 版本号和作者信息
        JLabel versionLabel = new JLabel("版本: 1.0.0");
        infoPanel.add(versionLabel);
        JLabel authorLabel = new JLabel("作者: adinlead");
        infoPanel.add(authorLabel);

        // 4. 按钮
        JPanel buttonPanel = new JPanel();
        JButton saveButton = new JButton("保存");
        JButton cancelButton = new JButton("取消");

        saveButton.addActionListener(e -> {
            saveSettings();
            settingsDialog.dispose();
        });

        cancelButton.addActionListener(e -> settingsDialog.dispose());

        buttonPanel.add(saveButton);
        buttonPanel.add(cancelButton);
        settingsDialog.add(buttonPanel);

        settingsDialog.setVisible(true);
    }

    private void saveSettings() {
        // 保存设置的逻辑
        System.out.println("保存设置");
    }

    public List<Option<String>> loadSounds() {
        List<Option<String>> sounds = new ArrayList<>();
        sounds.add(new Option<>("晨禧", Resource.SOUND_MORNING_JOY.path()));
        sounds.add(new Option<>("数字警报2", Resource.SOUND_DIGITAL_ALARM2.path()));
        sounds.add(new Option<>("威斯敏斯特钟声", Resource.SOUND_WESTMINSTER_CHIME.path()));
        // 指定要遍历的目录路径
        File directoryPath = new File("sounds/");
        File[] listFiles = directoryPath.listFiles();
        for (File soundFile : listFiles) {
            String soundFileName = soundFile.getName();
            if (soundFileName.endsWith(".mp3")) {
                sounds.add(new Option<>(soundFileName.replace(".mp3", ""), soundFile.getPath()));
            }
        }
        return sounds;
    }

    /**
     * 从资源文件和本地文件中加载语言
     *
     * @return 语言列表
     */
    public List<Option<String>> loadLocales() {
        Gson gson = new GsonBuilder().create();
        List<Option<String>> locales = new ArrayList<>();

        for (Resource resource : Resource.values()) {
            if (resource.name().startsWith("LANG_")) {
                try (InputStream stream = resource.stream()) {
                    try (InputStreamReader reader = new InputStreamReader(stream)) {
                        Locale locale = gson.fromJson(reader, Locale.class);
                        locales.add(new Option<>(locale.getLangName(), locale.getLangCode()));
                    }
                } catch (IOException e) {
                    Logger.error(e, "获取内部配置文件【{}】出错", resource.path());
                }
            }
        }

        // 指定要遍历的目录路径
        File directoryPath = new File("locale/");
        File[] listFiles = directoryPath.listFiles();
        if (listFiles != null) {
            for (File file : listFiles) {
                if (file.isFile() && file.getName().endsWith(".json")) {
                    try (FileReader reader = new FileReader(file)) {
                        Locale locale = gson.fromJson(reader, Locale.class);
                        locales.add(new Option<>(locale.getLangName(), locale.getLangCode()));
                    } catch (IOException e) {
                        Logger.error(e, "获取内部配置文件【{}】出错", file.getPath());
                    }
                }
            }
        }
        return locales;
    }
}
